#include "junix.h"
#include <boost/variant.hpp>

void print(boost::variant<int,std::string>& var)
{
	if( int* pi=boost::get<int>(&var))
	{
		var = *pi+*pi;
		std::cout<<var<<std::endl;
	}
	else if( std::string* ps=boost::get<std::string>(&var))
	{
		var = *ps+*ps;
		std::cout<<var<<std::endl;
	}
}

class times_two_visitor
    : public boost::static_visitor<>
{
	public:

		void operator()(int & i) const
		{
			i *= 2;
		}

		void operator()(std::string & str) const
		{
			str += str;
		}

};


class jtimes_two_visitor
:public boost::static_visitor<>
{
	public:
	template<typename T>
		void operator()(T& t) const
		{
			t += t;
		}
};

int main(int argc, char **argv)
{
	boost::variant<int,std::string> v;
	v=123;
	print(v);
	v=std::string("hello,world.");
	print(v);

	v=std::string("hello,world.");
	boost::apply_visitor( times_two_visitor(), v);
	std::cout<<v<<std::endl;

	v=std::string("hello,world.");
	boost::apply_visitor(jtimes_two_visitor(),v);
	std::cout<<v<<std::endl;

}
